<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

    //系统首页
    public function index(){
        $uid=is_login();
        $info=M('agent')->where('uid='.$uid)->find();
        $where['name']=array('in','WEB_SITE_DESCRIPTION,WEB_YUNLING_ADRESS');
        $data  =   M("Config")->where($where)->field('id,name,title,extra,value,remark,type')->find();
        $this->assign('info',$info);
        $this->assign('data',$data);
        $this->display();
    }


    /**
     * 竞价单列表
     */
    public function bidding_list(){
        //竞价中
        $where1['end_time']=array('gt',time());
        $list   = M('bid_list')->where($where1)->select();

        $where2['end_time']=array('lt',time());
        $list_end=M('bid_list')->where($where2)->select();

        $cateList = M("bid_type")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('list', $list);
        $this->assign('list_end', $list_end);
        $this->display();
    }


    //竞价详情页和竞价结束页都要执行的方法
    public function common_bidding($id){
        $uid=is_login();
        $user_data['user_sn']=getUSerBidSn($id,$uid);
        if(empty($user_data['user_sn'])){
            $user_data['bid_id']=$id;
            $user_data['user_id']=$uid;
            $user_data['user_sn']=createUserBidSn($id,$uid);
            M('bid_user_sn')->add($user_data);
        }

        $this->assign('user_sn', $user_data['user_sn']);

        //获取搜索条件信息
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);

        $brandList = M("brand")->select();
        $this->assign('brandList',$brandList);

        $modelList = M("model")->select();
        $this->assign('modelList',$modelList);
    }


    //专场竞价中要执行的
    public function bidding_cate2($id){

        //获取我的报价信息
        $my_bids=M('bid_user')->where(array('bid_id'=>$id,'user_id'=>is_login()))->select();
        $my_bids=get_id_key($my_bids,'good_id');

        //这场竞价中我预计得到的商品数和价格
        $yuji_count=0;
        $yuji_price=0;
        //获取超过我报价的人数
        $excess_count=0;
        foreach ($my_bids as $k=>&$v){
            $map['bid_id']=$id;
            $map['user_price']=array('gt',$v['user_price']);
            $map['good_id']=$v['good_id'];
            //获取单个商品报价高于我的人数
            $item_list=M('bid_user')->where($map)->select();

            $map['user_price']=$v['user_price'];

            //获取单个商品报价等于我的人数
            $item_list2=M('bid_user')->where($map)->order('create_time asc')->select();

            //如果每个商品有超过我的报价，那么这个产品我就不能竞得
            if(count($item_list)>0){
                $v['yuji_count']=0;
                $v['yuji_price']=0;
            }else{
                //如果有和我出价相同的，先出价者得
                if(count($item_list2)>0 && $item_list2[0]['user_id']!=$v['user_id']){
                    $v['yuji_count']=0;
                    $v['yuji_price']=0;
                }else{
                    $v['yuji_count']=$v['user_number'];
                    $v['yuji_price']=$v['user_price'];
                }
            }

            $excess_count+= count($item_list);

            //整场竞价中的预计得到的商品数和价格
            $yuji_count+= $v['yuji_count'];
            $yuji_price+=$v['yuji_price'];
        }
        $this->assign('my_bids', $my_bids);
        $this->assign('excess_count',$excess_count);
        $this->assign('yuji_count',$yuji_count);
        $this->assign('yuji_price',$yuji_price);
    }

    //通货竞价执行的
    public function bidding_cate1($id){
        //获取我的报价信息
        $my_bids=M('bid_user')->where(array('bid_id'=>$id,'user_id'=>is_login()))->find();

        $bid_users=M('bid_user')->where(array('bid_id'=>$id))->order('user_price desc,create_time asc')->select();

        if($bid_users[0]['user_id'] != is_login()){
            $status="未竞得";
        }else{
            $status="已竞得";
        }


        $this->assign('my_bids', $my_bids);
        $this->assign('status',$status);
    }

    /**
     * 竞价详情
     */
    public function bidding_info(){
        $id    =   I('get.id');
        if(empty($id)){
            $this->error('参数错误');
        }
        //获取当前竞价单信息
        $data=M('BidList')->where(array('id'=>$id))->find();
        $good_list=M('bid_good')->where(array('bid_id'=>$id))->select();

        if($data['end_time']<time()){
            $this->redirect('bidding_end',array('id'=>$id));
        }

        $this->common_bidding($id);

        switch ($data['cate_id']) {
            case '1':
                $tpl='info_cate1';
                break;
            
           case '2':
               foreach($good_list as $k=>&$v){
                   $where['bid_id']=$id;
                   $where['good_id']=$v['id'];
                   //每个产品的报价用户数
                   $user_list=M('bid_user')->where($where)->order('per_price desc,create_time asc')->select();
                   $v['user_count']=count($user_list);
                   $v['user_list']=$user_list;
               }
               $this->bidding_cate2($id);
                $tpl='info_cate2';
                break;
        }
        $this->assign('good_list', $good_list);
        $this->assign('data', $data);
        $this->assign('url',U('bidding_list'));

        $this->display($tpl);   
    }


     /**
     * 竞价结束详情页
     */
    public function bidding_end(){
        $id    =   I('get.id');
        if(empty($id)){
            $this->error('参数错误');
        }

        $data=M('BidList')->where(array('id'=>$id))->find();
        $good_list=M('bid_good')->where(array('bid_id'=>$id))->select();


        $this->common_bidding($id);

        switch ($data['cate_id']) {
            case '1':
                $this->bidding_cate1($id);
                $tpl='end_info1';
                break;
            case '2':
                foreach($good_list as $k=>&$v){
                    $where['bid_id']=$id;
                    $where['good_id']=$v['id'];
                    //每个产品的报价用户数
                    $user_list=M('bid_user')->where($where)->order('per_price desc,create_time asc')->select();
                    $v['user_count']=count($user_list);
                    $v['user_list']=$user_list;
                }
                $this->bidding_cate2($id);
                $tpl='end_info2';
                break;
        }
        $this->assign('good_list', $good_list);
        $this->assign('data', $data);
        $this->assign('url',U('bidding_list'));
        $this->display($tpl);   
    }


   //获取商品品牌
    public function ajaxGetGoodBrand(){
      $cate_id = I('get.cate_id') ? I('get.cate_id') : 0;  
      $brandlList = M("brand")->where(array('cate_id'=>$cate_id))->select();
      $this->assign('brandlList',$brandlList);
      $html=$this->fetch('ajaxgetgoodbrand');
      $this->ajaxReturn($html);
    }

   //获取商品型号
    public function ajaxGetGoodModel(){
      $brand_id = I('get.brand_id') ? I('get.brand_id') : 0;  
      $modelList = M("model")->where(array('brand_id'=>$brand_id))->select();
      $this->assign('modelList',$modelList);
      $html=$this->fetch('ajaxgetgoodmodel');
      $this->ajaxReturn($html);
    }

    public function ajaxPostUserBid(){
       $bid_type=I('post.bid_type');
       switch ($bid_type) {
             case '1':
                 $this->ajaxPostUserBidCate1();
                 break;
             case '2':
                 $this->ajaxPostUserBidCate2();
                 break;
       }        

     

    }

    //用户竞价-统货竞价
    private function ajaxPostUserBidCate1(){
        $data=$_POST;
        $data['user_id']=is_login();  
        $data['status']=1;
        $data['create_time']=time();

         //查看该商品的已存在最高报价，用户报价必须高于他
         $map['bid_id']=$data['bid_id'];
         $map['user_price']=array('gt',$data['user_price']);
          //每个产品的报价用户数
         $user_list=M('bid_user')->where($map)->order('user_price desc')->select();

         if(is_array($user_list) && $user_list[0]['user_price']>$data['user_price']){
             $this->ajaxReturn(['status'=>0,'msg'=>'本场目前最低报价为'+$user_list[0]['user_price']]);
             die();
         }

        //查看用户之前是否提交
        $map['user_id']=$data['user_id'];
        $user_bid=M('bid_user')->where($map)->field('id')->find();

        if(empty($user_bid['id'])){
            $res = M('bid_user')->add($data);
        }else{
            $res = M('bid_user')->where(array('id'=>$user_bid['id']))->save($data);
        }

        if(!$res){
            $this->ajaxReturn(['status'=>0,'msg'=>'提交失败']);
        }else{
            $this->ajaxReturn(['status'=>1,'msg'=>'提交成功']);
        }

    }

    //用户竞价-专场竞价
    private function ajaxPostUserBidCate2(){
        $data=I('post.');
        $data['user_id']=is_login();  
        $data['status']=1;
        $data['create_time']=time();
        $data['user_price']=$data['user_number']*$data['per_price'];


         $where['id']=$data['good_id'];
         $bid_good=M('bid_good')->where($where)->field('store_count')->find();
         if($data['user_number']>$bid_good['store_count']){
             $this->ajaxReturn(['status'=>0,'msg'=>'起拍数为不能超过库存']);
             die();
         }

         //查看该商品的已存在最高报价，用户报价必须高于他
         $map['bid_id']=$data['bid_id'];
         $map['good_id']=$data['good_id'];
          //每个产品的报价用户数
         $user_list=M('bid_user')->where($map)->order('per_price desc')->select();

         if(is_array($user_list) && $user_list[0]['per_rice']>=$data['per_price']){
             $this->ajaxReturn(['status'=>0,'msg'=>'本产品目前最低报价单价为'+$user_list[0]['per_rice']]);
             die();
         }

        //查看用户之前是否提交
        $map['user_id']=$data['user_id'];
        $user_bid=M('bid_user')->where($map)->field('id')->find();


        if(empty($user_bid['id'])){
            $res = M('bid_user')->add($data);
        }else{
            $res = M('bid_user')->where(array('id'=>$user_bid['id']))->save($data);
        }
       /* M('bid_good')->where($where)->setDec('store_count',$data['user_number']);*/

        if(!$res){
            $this->ajaxReturn(['status'=>0,'msg'=>'提交失败']);
        }else{
            $this->ajaxReturn(['status'=>1,'msg'=>'提交成功']);
        }
    }

   
    
    //搜索商品
    public function ajaxPostBidGood(){
      //获取当前竞价单  
      $id    =   I('get.id');
      $data=M('BidList')->where(array('id'=>$id))->find();

      $where=I('post.');
      $goods_ids=M('goods')->where($where)->getField('id',true);

      if(empty($goods_ids)){
         $this->ajaxReturn('');
      }

      $map['bid_id']=$id;
      $map['good_id']=array('in',$goods_ids);
      $good_list=M('bid_good')->where(array('bid_id'=>$id))->select();

      switch ($data['cate_id']) {
            case '1':
                $tpl='ajax_info1';
                break;
            case '2':
                $tpl='ajax_info2';
                break;
     }
      $this->assign('is_end',I('get.is_end'));  
      $this->assign('good_list', $good_list);
      $html=$this->fetch($tpl);
      $this->ajaxReturn($html);

    }

    //搜索竞价列表
    public function ajaxPostBidList(){

      $data=I('post.');
      $is_end=I('get.is_end');

      if($is_end){
          $bid_list=$this->ajaxPostBidListEnd($data);
          $tpl='bid_list1';
      }else{
        $bid_list=$this->ajaxPostBidListNotEnd($data);
        $tpl='bid_list2';
      }
      $this->assign('list', $bid_list);
      $html=$this->fetch($tpl);
      $this->ajaxReturn($html);
    }


    //搜索竞价没有结束的
    private function ajaxPostBidListNotEnd($data){
        if(!empty($data['bid_sn'])){
            $bid_list=M('bid_list')->where(array('bid_sn'=>$data['bid_sn']))->select();
        }else {
            if (!empty($data['end_time']) && strtotime($data['end_time']) >time()) {
                $end_time = strtotime($data['end_time']);
            } else {
                $end_time = time();
            }

            if (!empty($data['start_time'])) {
                $where['end_time'] = array('between', array(strtotime($data['start_time']), $end_time));
            } else {
                $where['end_time'] = array('gt', $end_time);
            }
            (!empty($data['cate_id'])) ? $where['cate_id'] = $data['cate_id'] : '';
            $bid_list = M('bid_list')->where($where)->select();
        }
        return $bid_list;
    }

    //搜索竞价结束的
    private function ajaxPostBidListEnd($data){
        if(!empty($data['bid_sn'])){
            $bid_list=M('bid_list')->where(array('bid_sn'=>$data['bid_sn']))->select();
        }else {
            if (!empty($data['end_time']) && strtotime($data['end_time']) < time()) {
                $end_time = strtotime($data['end_time']);
            } else {
                $end_time = time();
            }

            if (!empty($data['start_time'])) {
                $where['end_time'] = array('between', array(strtotime($data['start_time']), $end_time));
            } else {
                $where['end_time'] = array('lt', $end_time);
            }
            (!empty($data['cate_id'])) ? $where['cate_id'] = $data['cate_id'] : '';
            $bid_list = M('bid_list')->where($where)->select();
        }
        return $bid_list;
    }


    //修改联系方式和地址
    public function editadd(){
        $uid=is_login();
        if(IS_POST){
            $data=I('post.');
            $re=M('agent')->where('uid='.$uid)->save($data);
            if($re!==false){
                $this->success('提交成功',U('Index/index'),true);
            }else{
                $this->error('提交失败！','',true);
            }
        }else{
            $info=M('agent')->where('uid='.$uid)->find();
            $this->assign('url',U('Index/index'));
            $this->assign('info',$info);
            $this->display();
        }
    }


}